package com.qupai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qupai.context.BaseContext;
import com.qupai.dto.ShoppingCartDTO;
import com.qupai.entity.Prizes;
import com.qupai.entity.ShoppingCart;
import com.qupai.mapper.PrizesMapper;
import com.qupai.mapper.ShoppingCartMapper;
import com.qupai.service.ShoppingCartService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper,ShoppingCart> implements ShoppingCartService {

    @Resource
    private ShoppingCartMapper shoppingCartMapper;
    @Resource
    private PrizesMapper prizesMapper;

    /**
     * 添加购物车
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //判断当前加入购物车的商品是否存在了
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //获取当前用户id
        Long userId= BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        //调用dao层查询当前购物车数据
        List<ShoppingCart> list=shoppingCartMapper.list(shoppingCart);

        //判断当前加入购物车的商品是否存在了
        //存在则数量+1
        if (!list.isEmpty()){
            //查到的数据只能是1条，get(0)得到查到的数据
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber()+1);
            //根据id更新商品数量
            shoppingCartMapper.updateById(cart);
        }
        //如果不存在，则新增
        else {
            //如果不存在，需要插入一条购物车数据
            Long prizesId = shoppingCartDTO.getPrizesId();
            if (prizesId != null){
                //本次添加到购物车的是奖品
                Prizes prizes = prizesMapper.selectById(prizesId);
                shoppingCart.setName(prizes.getName());
                shoppingCart.setImage(prizes.getImage());
                shoppingCart.setAmount(prizes.getPrice());
                shoppingCart.setNumber(1L);
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCart.setDescription(prizes.getDescription());
                shoppingCartMapper.insert(shoppingCart);
            }
        }
    }

    /**
     * 查看购物车
     */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        Long userId=BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(userId)
                .build();
        return shoppingCartMapper.list(shoppingCart);
    }

    /**
     *  清空购物车
     */
    @Override
    public void cleanShoppingCart() {
        //获取到当前微信用户的id获取到当前微信用户的id
        Long userId = BaseContext.getCurrentId();
        QueryWrapper<ShoppingCart> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(ShoppingCart::getUserId,userId);
        shoppingCartMapper.delete(wrapper);
    }

    /**
     * 删除购物车中一个商品
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart>list=shoppingCartMapper.list(shoppingCart);

        if (list!=null&& !list.isEmpty()){
            shoppingCart=list.get(0);
            Long number=shoppingCart.getNumber();
            if (number==1){
                //当前商品在购物车中的份数为1，直接删除当前记录
                shoppingCartMapper.deleteById(shoppingCart.getId());
            }else {
                //商品数量减一
                shoppingCart.setNumber(number-1);
//                shoppingCartMapper.updateNumberById(shoppingCart);
                shoppingCartMapper.updateById(shoppingCart);
            }
        }
    }

    /**
     * 批量删除购物车中的奖品
     */
    @Override
    public void deleteBatch(List<Long> shoppingCartIds) {
        for (Long shoppingCartId : shoppingCartIds) {
            ShoppingCart shoppingCart=new ShoppingCart();
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setId(shoppingCartId);
            List<ShoppingCart>list=shoppingCartMapper.list(shoppingCart);
            if (list!=null&& !list.isEmpty()) {
                //根据用户id和购物车id删除购物车中的奖品
                QueryWrapper<ShoppingCart>wrapper=new QueryWrapper<>();
                wrapper.lambda()
                        .eq(ShoppingCart::getUserId,shoppingCart.getUserId())
                        .eq(ShoppingCart::getId,shoppingCartId);
                shoppingCartMapper.delete(wrapper);
            }
        }
    }


}
